﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ATM.BUS;
using ATM.Entities;
namespace ATMSystem
{
    public partial class frmChangePIN : Form
    {
        private Account passAcc;
        private Card passCard;
        string pin1 = "";
        string pin2 = "";

        private bool CheckInPut(string s)
        {
            return s.Count() == 6;
        }
        public frmChangePIN()
        {
            InitializeComponent();
        }

        public frmChangePIN(Account acc,Card card)
        {
            InitializeComponent();
            passAcc = acc;
            passCard = card;
        }

        private void btnCancel1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnClear1_Click(object sender, EventArgs e)
        {
            txtOldPIN1.Text = "";
            txtOldPIN1.Focus();
        }

        private void btnEnter1_Click(object sender, EventArgs e)
        {
            if (!ChangePinBUS.CheckOldPin(passCard.CardNo, txtOldPIN1.Text.Trim()))
            {
                panelEnterOldPin.Visible = false;
                panelErrorEnterOldPin.Visible = true;
            }
            else
            {
                panelEnterOldPin.Visible = false;
                panelEnterNewPin.Visible = true;
            }
        }

        private void btnCancel2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnClear2_Click(object sender, EventArgs e)
        {
            txtOldPIN2.Text = "";
            txtOldPIN2.Focus();
        }

        private void btnEnter2_Click(object sender, EventArgs e)
        {
            if (ChangePinBUS.CheckOldPin(passCard.CardNo, txtOldPIN2.Text.Trim()))
            {
                panelErrorEnterOldPin.Visible = false;
                panelEnterNewPin.Visible = true;
            }
            else
            {
                txtOldPIN2.Text = "";
                txtOldPIN2.Focus();
                label6.Focus();
            }
        }

        private void btnCancel3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnClear3_Click(object sender, EventArgs e)
        {
            txtNewPin1.Text = "";
            txtNewPin1.Focus();
        }

        private void btnEnter3_Click(object sender, EventArgs e)
        {
            if (CheckInPut(txtNewPin1.Text.Trim()))
            {
                pin1 = txtNewPin1.Text.Trim();
                panelRe_EnterNewPin.Visible = true;
                panelEnterNewPin.Visible = false;
            }
            else
            {
                labelInPutFalse3.BackColor = Color.Red;
                labelInPutFalse3.Focus();
                txtNewPin1.Focus();
            }
        }

        private void btnCancel4_Click(object sender, EventArgs e)
        {
            this.Close();

        }

        private void btnClear4_Click(object sender, EventArgs e)
        {
            txtNewPin2.Text = "";
            txtNewPin2.Focus();
        }

        private void btnEnter4_Click(object sender, EventArgs e)
        {
            if (!CheckInPut(txtNewPin2.Text.Trim()))
            {
                labelInPutFalse4.BackColor = Color.Red;
                labelInPutFalse4.Focus();
                txtNewPin2.Focus();
            }
            else
            {
                pin2 = txtNewPin2.Text.Trim();
                if (pin1 != pin2)
                {
                    panelErrorEnterNewPin.Visible = true;
                    panelRe_EnterNewPin.Visible = false;
                }
                else
                {
                    if (ChangePinBUS.UpdatePIN(passCard.CardNo, txtNewPin2.Text.Trim()) > 0)
                        panelChangedPin.Visible = true;

                }

            }
        }

        private void btnCancel5_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnClear5_Click(object sender, EventArgs e)
        {
            txtNewPin3.Text = "";
            txtNewPin3.Focus();
        }

        private void btnEnter5_Click(object sender, EventArgs e)
        {
            if (!CheckInPut(txtNewPin3.Text.Trim()))
            {
                labelInPutFalse5.BackColor = Color.Red;
                labelInPutFalse5.Focus();
                txtNewPin3.Focus();
            }
            else
            {
                pin2 = txtNewPin3.Text.Trim();
                if (pin1 != pin2)
                {
                    panelErrorEnterNewPin.Visible = true;
                    panelRe_EnterNewPin.Visible = false;
                    labelNotMatch.Visible = true;
                }
                else
                {
                    if (ChangePinBUS.UpdatePIN(passCard.CardNo, txtNewPin2.Text.Trim()) > 0)
                        panelChangedPin.Visible = true;

                }

            }
        }

        private void btnOK6_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmChangePIN_Load(object sender, EventArgs e)
        {
            panelEnterNewPin.Visible = false;
            panelErrorEnterOldPin.Visible = false;
            panelRe_EnterNewPin.Visible = false;
            panelErrorEnterNewPin.Visible = false;
            panelChangedPin.Visible = false;
        }
    }
}
